﻿namespace WebTest
{
    using QRCode;
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;

    public static class SendMessage
    {
        private static Encoding encode = Encoding.GetEncoding("gb2312");

        public static string GetBalance(string strUserID, string strUserPwd)
        {
            string result = "";
            string url = "http://118.144.76.79:8888/mmsServer2/queryBalance";
            string postdata = "user_id=" + strUserID + "&user_pwd=" + strUserPwd;
            result = PostSend(url, postdata);
            switch (result)
            {
                case "-2":
                    return "-1";

                case "null#":
                    return "0";
            }
            return result.Substring(result.Length - 1, 1);
        }

        public static string PostSend(string url, string postdate)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.Method = "POST";
            Stream myRequestStream = myHttpWebRequest.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postdate);
            myStreamWriter.Flush();
            myStreamWriter.Close();
            myRequestStream.Close();
            Stream myResponseStream = ((HttpWebResponse)myHttpWebRequest.GetResponse()).GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
            string outdata = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return outdata;
        }

        public static string SendMMSToWG(string id, string pwd, string Mobile, string Subject, string Content)
        {
            string result = "";
            string subject = HexConvert.StringToHexString(Subject, encode);
            string d1 = Convert.ToString(1);
            string tt1 = "txt";
            string tv1 = HexConvert.GetHexString(HexConvert.FileToBytes(HttpContext.Current.Server.MapPath("") + @"\Readme.txt"));
            string pt1 = "jpg";
            string pv1 = HexConvert.GetHexString(QRCodeImage.imageToByte(Content));
            string url = "http://118.144.76.79:8080/mmsServer/sendMms";
            string postdata = "id=" + id + "&pwd=" + pwd + "&subject=" + subject + "&d1=" + d1 + "&tt1=" + tt1 + "&tv1=" + tv1 + "&pt1=" + pt1 + "&pv1=" + pv1 + "&mt1=&mv1=";
            string mms_id = PostSend(url, postdata);
            if ((mms_id != null) & (int.Parse(mms_id) > 0))
            {
                url = "http://118.144.76.79:8080/mmsServer/sendMobile";
                postdata = "id=" + id + "&pwd=" + pwd + "&yw=10690029yd&mobile=" + Mobile + "&mms_id=" + mms_id;
                result = PostSend(url, postdata);
            }
            return result;
        }
    }

}
